package ui.activity.poscontrol.presenter;


import com.google.gson.Gson;

import java.util.ArrayList;
import java.util.List;

import javax.inject.Inject;

import Utils.SharedPrefStore;
import base.BaseBiz;
import dao.FlowDevicesInput;
import okhttp3.MediaType;
import okhttp3.RequestBody;
import ui.activity.poscontrol.beanmodel.MyPosBean;
import ui.activity.poscontrol.beanmodel.PosInfoBean;
import ui.activity.poscontrol.biz.MyPosControlBiz;
import ui.activity.poscontrol.contract.MyPosControlContract;

/**
 * Created by chenjian on 19/2/26
 */

public class MyPosControlPresenter implements MyPosControlContract.Presenter {

    MyPosControlContract.View view;
    MyPosControlBiz myPosControlBiz;
    List<MyPosBean.RecordsBean> recordsBeans = new ArrayList<>();
    List<PosInfoBean.RecordsBean> posInfobean = new ArrayList<>();
    private int page = 0;
    private int size = 100;
    @Inject
    public MyPosControlPresenter(MyPosControlContract.View view) {
        this.view = view;
    }


    @Override
    public void setBiz(BaseBiz biz) {
        myPosControlBiz = (MyPosControlBiz) biz;
    }


    @Override
    public void getUnActiveList( String customId,boolean isRefresh,String desc,String esc) {
        view.loading(true);
        if(isRefresh){
            recordsBeans.clear();
            page = 1;
        }else{
            page+=1;
        }
        myPosControlBiz.getUnactiveList(customId,page,size,desc,esc, new BaseBiz.Callback<MyPosBean>() {
            @Override
            public void onSuccess(MyPosBean info) {

                recordsBeans.addAll(info.getRecords());
                view.upDateUI(info,recordsBeans,isRefresh);
                if (isRefresh) {
                    view.refreshComplete();
                } else {
                    view.loadMoreComplete();
                }
                if(size==info.getRecords().size()) {
                    view.hasLoadMore(true);
                }else{
                    view.hasLoadMore(false);
                }
                view.loading(false);
            }

            @Override
            public void onFailure(MyPosBean info) {
                view.loading(false);
                if (isRefresh) {
                    view.refreshComplete();
                } else {
                    view.loadMoreComplete();
                }
            }
        });
    }

    @Override
    public void getPosInfoList(String customId, int activeStatus,boolean isRefresh) {
        view.loading(true);
        if(isRefresh){
            posInfobean.clear();
            page = 1;
        }else{
            page+=1;
        }
        myPosControlBiz.getPosInfoList(customId,page,size,activeStatus, new BaseBiz.Callback<PosInfoBean>() {
            @Override
            public void onSuccess(PosInfoBean info) {
                posInfobean.addAll(info.getRecords());
                view.upDateInfoListUI(info,posInfobean);
                if(size==info.getRecords().size()) {
                    view.hasLoadMore(true);
                }else{
                    view.hasLoadMore(false);
                }
                if (isRefresh) {
                    view.refreshComplete();
                } else {
                    view.loadMoreComplete();
                }



                view.loading(false);
            }

            @Override
            public void onFailure(PosInfoBean info) {
                view.loading(false);
                    view.loadMoreComplete();
            }
        });
    }


    public void flowDevices(Integer toId, List<String> noList, int backAmount) {
        SharedPrefStore store = new SharedPrefStore();
        int id = Integer.valueOf(store.getString("id", "0"));
        FlowDevicesInput input = new FlowDevicesInput();
        input.fromId = id;
        input.toId = toId;
        input.snList = new String[noList.size()];
        input.backAmount = backAmount;
        for (int i = 0; i < noList.size(); i++) {
            input.snList[i] = noList.get(i);
        }
        RequestBody requestBody = RequestBody.create(MediaType.parse("application/json"), new Gson().toJson(input));
        myPosControlBiz.FlowDevices(requestBody, new BaseBiz.Callback<Object>() {
            @Override
            public void onSuccess(Object info) {
                view.showMsg("划拨成功");
                view.refreshData();
            }

            @Override
            public void onFailure(Object info) {
            }
        });
    }
}
